import router from "@/router/index.js";
import {asyncRoutes} from "@/router/index.js";
import {validateNull} from "@/util/validate.js";

const tabsView = {
    state: () => ({
        tabs: asyncRoutes.filter(item=>!validateNull(item.meta)&&item.meta.affix).map(item=>{return {name: item.name, url: item.path, affix: true}})
    }),
    mutations: {
        ADD_TAB(state, tab) {
            if (state.tabs.some(item => item.url === tab.url)) return
            state.tabs.push(tab)
        },
        REMOVE_TAB(state, url){
            const index = state.tabs.findIndex(item=>item.url===url)
            if(index===-1)return
            state.tabs.splice(index,1)
            console.log(state.tabs[state.tabs.length-1].url)
            router.push(state.tabs[state.tabs.length-1].url).then()
        }

    },
    actions: {},
    getters: {
        tabs: state => state.tabs
    }
}
export default tabsView